#聊天室登录页 测试用例
import time

from selenium.webdriver.common.by import By

from common.Utils import ChatDriver

class ChatLogin:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://127.0.0.1:8080/login.html"
        self.driver = ChatDriver.driver
        self.driver.get(self.url)

    # 成功登录_测试用例
    def LoginSucTest(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        ChatDriver.getScreenshot()
        time.sleep(2)
        # 处理弹窗
        alert = self.driver.switch_to.alert
        print(alert.text)
        alert.accept()
        #返回登录页面
        self.driver.back()
    # 异常登录_测试用例(用户名正确，密码错误）
    def LoginFailTest1(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsan")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("1234")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        ChatDriver.getScreenshot()
        time.sleep(2)
        # 处理弹窗
        alert = self.driver.switch_to.alert
        print(alert.text)
        alert.accept()

    # 异常登录_测试用例（用户名错误）
    def LoginFailTest2(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("zhangsi")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        ChatDriver.getScreenshot()
        time.sleep(2)
        # 处理弹窗
        alert = self.driver.switch_to.alert
        print(alert.text)
        alert.accept()

    # 异常登录_测试用例（用户名、密码为空）
    def LoginFailTest3(self):
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys(" ")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys(" ")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        ChatDriver.getScreenshot()
        time.sleep(2)
        # 处理弹窗
        alert = self.driver.switch_to.alert
        print(alert.text)
        alert.accept()